<?php

/**
 * 
 * 分类广告
 *  
 */
class CatadAction extends HomeAction {
    
    public function _initialize() {
        parent::_initialize();
        $this->seo('分类广告 ');
    }

    function index() {
        //地区列表
        $areas = M('Area')->where(array('status' => 1))->select();
        //分类
        $cat     = D('Cat')
                ->field("*,cat_id as id,parent_id as parentid")
                ->where(array('parent_id' => 0))
                ->order(array("sort_order" => "ASC"))
                ->select();
        $aid         = format_int($_REQUEST['aid']);
        $cid         = format_int($_REQUEST['cid']);
        $left_cid    = format_int($_REQUEST['ccid']);
        //左侧分类
        if (!$cid) {
            $left_cat = D('Cat')
                    ->field("*,cat_id as id,parent_id as parentid")
                    ->where(array('parent_id' => array('neq', 0)))
                    ->order(array("sort_order" => "ASC"))
                    ->select();
        } else {
            $left_cat = D('Cat')
                    ->field("*,cat_id as id,parent_id as parentid")
                    ->where(array('parent_id' => $cid))
                    ->order(array("sort_order" => "ASC"))
                    ->select();
        }
        //新增加每个分类下面的全部分类
        $all_cat = $left_cat[0];
        $all_cat['name'] = '全部';
        $all_cat['cat_id'] = 999999999;
        array_unshift($left_cat, $all_cat);
        
        //点左侧分类
        $catadModel  = M('Catad');
        $order       = 'create_time DESC';
        $left_cid    = ($left_cid && $left_cid != $all_cat['cat_id']) ? $left_cid : array('IN', getSubByKey($left_cat, 'cat_id'));
        if ($left_cid) {
            //分类广告
            $limit   = 10;
            $wheread = array(
                'cat_id'   => $left_cid,
                'end_time' => array('egt', time()),
                'status_check'=> 'true',
            );
            if ($aid) {
                $wheread['area_id'] = $aid;
            }
            //取得满足条件的记录数
            $count              = $catadModel->where($wheread)->count($catadModel->getPk());
            if ($count > 0) {
                import("ORG.Util.Page");
                //创建分页对象
                $p = new Page($count, $limit);
                //分页查询数据

                $voList = $catadModel->where($wheread)->order($order)->limit($p->firstRow . ',' . $p->listRows)->select();
                //分页显示
                $page   = $p->show();
                //模板赋值显示
                $data   = array(
                    'list'      => $voList,
                    'page'      => $page,
                    'nowPage'   => $_REQUEST['page'],
                    'totalPage' => round($count / $limit),
                );
            }
            $this->assign('left', $left_cat);
            $this->assign('data', $data);
            $this->assign('area', $areas);
            $this->assign('cat', $cat);
            $this->display();
        } else {
            foreach ($left_cat as $lk => $lv) {
                $wheread = array(
                    'cat_id'   => $lv['cat_id'],
                    'end_time' => array('egt', time()),
                );
                $left_cat[$lk]['ads'] = $catadModel->where($wheread)->limit(2)->order($order)->select();
            }
            $this->assign('left', $left_cat);
            $this->assign('area', $areas);
            $this->assign('cat', $cat);
            $this->display('index_cat');
        }
    }

}
